//package com.hhf.feiojcommonservice.aop;
//
//
//import com.hhf.feiojapiservice.client.UserFeignClient;
//import com.hhf.feiojcommonservice.annotation.AuthCheck;
//import com.hhf.feiojcommonservice.common.ErrorCode;
//import com.hhf.feiojcommonservice.exception.BusinessException;
//import com.hhf.feiojcommonservice.model.entity.User;
//import com.hhf.feiojcommonservice.model.enums.RoleEnum;
//import com.hhf.feiojcommonservice.utils.UserIdContext;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * 权限校验 AOP
// *
// * @author hhf
// * @data 2024/9/26
// */
//@Aspect
//@Component
//public class AuthInterceptor {
//
//
//    /**
//     * 执行拦截
//     *
//     * @param joinPoint
//     * @param authCheck
//     * @return
//     */
//    @Around("@annotation(authCheck)")
//    public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
//        String mustRole = authCheck.mustRole();
//        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
//        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
//        // 当前登录用户
//        Long userId = UserIdContext.getUserId();
//        User loginUser = userService.getLoginUser(request);
//        RoleEnum mustRoleEnum = RoleEnum.getEnumByValue(mustRole);
//        // 不需要权限，放行
//        if (mustRoleEnum == null) {
//            return joinPoint.proceed();
//        }
//        // 必须有该权限才通过
//        RoleEnum roleEnum = RoleEnum.getEnumByValue(loginUser.getRole());
//        if (roleEnum == null) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        // 如果被封号，直接拒绝
//        if (RoleEnum.BAN.equals(roleEnum)) {
//            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//        }
//        // 必须有管理员权限
//        if (RoleEnum.ADMIN.equals(mustRoleEnum)) {
//            // 用户没有管理员权限，拒绝
//            if (!RoleEnum.ADMIN.equals(roleEnum)) {
//                throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
//            }
//        }
//        // 通过权限校验，放行
//        return joinPoint.proceed();
//    }
//}
//
